package sy.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sy.dao.WYAccountDaoI;
import sy.model.SysInformation;
import sy.model.SysProject;
import sy.model.SysProjectInformationMapping;
import sy.model.WYAccount;
import sy.model.WYAccountProjectMapping;
import sy.service.WYAccountProjectMappingServiceI;
import sy.service.WYAccountServiceI;
import sy.util.Encrypt;
@Service("wyAccountService")
public class WYAccountServiceImpl implements WYAccountServiceI{
	@Autowired
	WYAccountDaoI wyAccountDao;
	@Autowired
	WYAccountProjectMappingServiceI wyAccountProjectMappingService;
	
	
	private void writeIntoWYAccountProjectMapping(WYAccount wyAccount,String[] projectIds){
		/**
		 * 构造中间表信息
		 * 写入物业账户项目中间表
		 */
		List<WYAccountProjectMapping> list = new ArrayList<WYAccountProjectMapping>();
		for(String proId : projectIds ){
			WYAccountProjectMapping wyAccontPro = new WYAccountProjectMapping();
			if(proId!=null && !"null".equals(proId)){
				SysProject pro = new SysProject();
				pro.setId(proId);
				wyAccontPro.setSysProject(pro);
				wyAccontPro.setWyAccount(wyAccount);
				wyAccontPro.setId(UUID.randomUUID().toString());
				list.add(wyAccontPro);
			}
		}
		wyAccountProjectMappingService.saveList(list);
	}

	@Override
	public void save(WYAccount wyAccount, String[] projectIds) {
		// TODO Auto-generated method stub
		wyAccount.setToken(UUID.randomUUID().toString());
		wyAccount.setPassword(Encrypt.md5(wyAccount.getPassword()));
		wyAccountDao.save(wyAccount);
		/**
		 * 构造中间表信息
		 * 写入物业账户项目中间表
		 */
		writeIntoWYAccountProjectMapping(wyAccount,projectIds);
	}

	@Override
	public void deleteByIds(List<String> ids) {
		// TODO Auto-generated method stub
		wyAccountDao.deleteByIds(ids);
	}

	@Override
	public void modify(WYAccount wyAccount,String[] projectIds) {
		// TODO Auto-generated method stub
		wyAccount.setPassword(Encrypt.md5(wyAccount.getPassword()));
		wyAccountDao.modify(wyAccount);
		/**
		 * 先删除该用户所有的项目，再插入
		 */
		wyAccountProjectMappingService.deleteByWYAccountId(wyAccount.getId());
		writeIntoWYAccountProjectMapping(wyAccount,projectIds);
	}

	@Override
	public WYAccount getWYAccountById(String id) {
		// TODO Auto-generated method stub
		return wyAccountDao.getWYAccountById(id);
	}

	@Override
	public List<WYAccount> listWYAccount() {
		// TODO Auto-generated method stub
		return wyAccountDao.listWYAccount();
	}

	@Override
	public WYAccount getLogin(String workCode, String password) {
		// TODO Auto-generated method stub
		return wyAccountDao.getLogin(workCode, password);
	}

	@Override
	public void uodateLoginTime(String token) {
		// TODO Auto-generated method stub
		wyAccountDao.uodateLoginTime(token);
	}

	@Override
	public WYAccount getWYAccountByToken(String token) {
		// TODO Auto-generated method stub
		return wyAccountDao.getWYAccountByToken(token);
	}

	@Override
	public void modifyPassword(String workCode, String password) {
		// TODO Auto-generated method stub
		wyAccountDao.modifyPassword(workCode, password);
	}

	@Override
	public WYAccount getWYAccountByCondition(String workCode, String telephone) {
		// TODO Auto-generated method stub
		return wyAccountDao.getWYAccountByCondition(workCode, telephone);
	}

}
